// $codepro.audit.disable numericLiterals, spellCheckIdentifiers, packagePrefixNamingConvention
// $codepro.audit.disable spellCheckComments
/**
 * Package gui
 */
package teamrocket.gui;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.DefaultComboBoxModel;

import teamrocket.domain.Doctor;
import teamrocket.domain.Nurse;
import teamrocket.domain.Patient;
import teamrocket.domain.SystemAdmin;
import teamrocket.domain.User;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Gui for Adding a User.
 * @author James Zhang, Juan Llanes, Stephen Tapper, Will Barr
 * @version 1.0
 */
public class AddUserFrame extends JFrame {

	/**
	 * Field contentPane.
	 */
	private final JPanel contentPane;

	/**
	 * Field tbFirst.
	 */
	private final JTextField tbFirst;

	/**
	 * Field tbLast.
	 */
	private final JTextField tbLast;

	/**
	 * Field tbUsername.
	 */
	private final JTextField tbUsername;

	/**
	 * Field tbPassword.
	 */
	private final JTextField tbPassword;

	/**
	 * Field cbUserType.
	 */
	private final JComboBox cbUserType;

	/**
	 * Field mainFrame.
	 */
	private final MainFrame mainFrame;

	/**
	 * Create the frame.
	 * 
	 * @param mainFrame
	 *            MainFrame
	 */
	public AddUserFrame(final MainFrame mainFrame) { // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.constructorsOnlyInvokeFinalMethods

		this.mainFrame = mainFrame;

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 261, 249);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		final JLabel label = new JLabel("First Name:");
		label.setBounds(10, 39, 76, 14);
		contentPane.add(label);

		tbFirst = new JTextField();
		tbFirst.setColumns(10);
		tbFirst.setBounds(110, 36, 130, 20);
		contentPane.add(tbFirst);

		final JLabel label_1 = new JLabel("Last Name:");
		label_1.setBounds(10, 64, 76, 14);
		contentPane.add(label_1);

		tbLast = new JTextField();
		tbLast.setColumns(10);
		tbLast.setBounds(110, 61, 130, 20);
		contentPane.add(tbLast);

		final JLabel label_2 = new JLabel("Username:");
		label_2.setBounds(10, 89, 59, 14);
		contentPane.add(label_2);

		tbUsername = new JTextField();
		tbUsername.setColumns(10);
		tbUsername.setBounds(110, 86, 130, 20);
		contentPane.add(tbUsername);

		tbPassword = new JTextField();
		tbPassword.setColumns(10);
		tbPassword.setBounds(110, 111, 130, 20);
		contentPane.add(tbPassword);

		final JLabel label_3 = new JLabel("Password:");
		label_3.setBounds(10, 114, 59, 14);
		contentPane.add(label_3);

		final JLabel label_4 = new JLabel("User Type:");
		label_4.setBounds(10, 139, 85, 14);
		contentPane.add(label_4);

		cbUserType = new JComboBox();
		cbUserType.setModel(new DefaultComboBoxModel(new String[] { "Patient",
				"Nurse", "Doctor", "System Administrator" }));
		cbUserType.setBounds(110, 142, 130, 20);
		contentPane.add(cbUserType);

		final JButton btnSave = new JButton("Save Changes");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent arg0) {
				createUser();
			}
		});
		btnSave.setBounds(110, 169, 130, 23);
		contentPane.add(btnSave);

		final JLabel lblAddNewUser = new JLabel("Add New User to System");
		lblAddNewUser.setBounds(10, 11, 225, 14);
		contentPane.add(lblAddNewUser);

		final JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent arg0) {
				cancel();
			}
		});
		btnCancel.setBounds(10, 169, 85, 23);
		contentPane.add(btnCancel);
	}

	/**
	 * Method createUser.
	 */
	private void createUser() {
		final String username = tbUsername.getText();
		final String first = tbFirst.getText();
		final String last = tbLast.getText();
		final String password = tbPassword.getText();
		String usertype = ""; // $codepro.audit.disable
		User user = null;
		if (cbUserType.getSelectedIndex() == 0) {
			usertype = "p";
			user = Patient.createBlankPatient(first, last, "");

			((Patient) user).createPatient();

		} else if (cbUserType.getSelectedIndex() == 1) {
			usertype = "n";
			user = new Nurse(username, password, first, last);
			user.create(); // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.unusedReturnValue
		} else if (cbUserType.getSelectedIndex() == 2) {
			usertype = "d";
			user = new Doctor(username, password, first, last);
			user.create(); // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.unusedReturnValue
		} else if (cbUserType.getSelectedIndex() == 3) {
			usertype = "a";
			user = new SystemAdmin(username, password, first, last);
			user.create(); // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.unusedReturnValue
		}

		mainFrame.rebind();
		mainFrame.setVisible(true);
		this.setVisible(false);
		mainFrame.rebindUsers(user);
	}

	/**
	 * Method cancel.
	 */
	private void cancel() {
		mainFrame.setVisible(true);
		this.setVisible(false);
	}
}
